﻿
namespace Coproject.Web.Services
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.ComponentModel.DataAnnotations;
	using System.Data;
	using System.Linq;
	using System.ServiceModel.DomainServices.EntityFramework;
	using System.ServiceModel.DomainServices.Hosting;
	using System.ServiceModel.DomainServices.Server;
	using Coproject.Web.Models;


	// Implements application logic using the CoprojectEntities context.
	// TODO: Add your application logic to these methods or in additional methods.
	// TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
	// Also consider adding roles to restrict access as appropriate.
	// [RequiresAuthentication]
	[EnableClientAccess()]
	public class CoprojectService : LinqToEntitiesDomainService<CoprojectEntities>
	{

		// TODO:
		// Consider constraining the results of your query method.  If you need additional input you can
		// add parameters to this method or create additional query methods with different names.
		// To support paging you will need to add ordering to the 'ToDoItems' query.
		public IQueryable<ToDoItem> GetToDoItems()
		{
			return this.ObjectContext.ToDoItems.Include("User");
		}

		public void InsertToDoItem(ToDoItem toDoItem)
		{
			if ((toDoItem.EntityState != EntityState.Detached))
			{
				this.ObjectContext.ObjectStateManager.ChangeObjectState(toDoItem, EntityState.Added);
			}
			else
			{
				this.ObjectContext.ToDoItems.AddObject(toDoItem);
			}
		}

		public void UpdateToDoItem(ToDoItem currentToDoItem)
		{
			this.ObjectContext.ToDoItems.AttachAsModified(currentToDoItem, this.ChangeSet.GetOriginal(currentToDoItem));
		}

		public void DeleteToDoItem(ToDoItem toDoItem)
		{
			if ((toDoItem.EntityState == EntityState.Detached))
			{
				this.ObjectContext.ToDoItems.Attach(toDoItem);
			}
			this.ObjectContext.ToDoItems.DeleteObject(toDoItem);
		}

		// TODO:
		// Consider constraining the results of your query method.  If you need additional input you can
		// add parameters to this method or create additional query methods with different names.
		// To support paging you will need to add ordering to the 'ToDoLists' query.
		public IQueryable<ToDoList> GetToDoLists()
		{
			return this.ObjectContext.ToDoLists;
		}

		public IQueryable<ToDoList> GetToDoListsWithItems()
		{
			return this.ObjectContext.ToDoLists.Include("ToDoItems");
		}

		public void InsertToDoList(ToDoList toDoList)
		{
			if ((toDoList.EntityState != EntityState.Detached))
			{
				this.ObjectContext.ObjectStateManager.ChangeObjectState(toDoList, EntityState.Added);
			}
			else
			{
				this.ObjectContext.ToDoLists.AddObject(toDoList);
			}
		}

		public void UpdateToDoList(ToDoList currentToDoList)
		{
			this.ObjectContext.ToDoLists.AttachAsModified(currentToDoList, this.ChangeSet.GetOriginal(currentToDoList));
		}

		public void DeleteToDoList(ToDoList toDoList)
		{
			if ((toDoList.EntityState == EntityState.Detached))
			{
				this.ObjectContext.ToDoLists.Attach(toDoList);
			}
			this.ObjectContext.ToDoLists.DeleteObject(toDoList);
		}

		// TODO:
		// Consider constraining the results of your query method.  If you need additional input you can
		// add parameters to this method or create additional query methods with different names.
		// To support paging you will need to add ordering to the 'Users' query.
		public IQueryable<User> GetUsers()
		{
			return this.ObjectContext.Users;
		}
	}
}


